package com.somnus.androidkotlindemo.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.design.widget.Snackbar
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import com.somnus.androidkotlindemo.R
import com.somnus.androidkotlindemo.adapter.RecyclerViewAdapter
import com.somnus.androidkotlindemo.bean.UserBean
import kotlinx.android.synthetic.main.activity_drawer.*
import kotlinx.android.synthetic.main.activity_recycler_view.*
import kotlinx.android.synthetic.main.app_bar_drawer.*


class DrawerActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_drawer)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }

        val toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

        nav_view.setNavigationItemSelectedListener(this)


        toolbar.setNavigationOnClickListener {
            if (!drawer_layout.isDrawerOpen(setDrawerGravity())) {
                drawer_layout.openDrawer(setDrawerGravity())
            }
        }


        val adapter = RecyclerViewAdapter(initData(), this)
        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = adapter
    }

    private fun setDrawerGravity(): Int {
        return Gravity.START
    }

    private fun initData(): List<UserBean> {
        var list = ArrayList<UserBean>()
        for (i in 0..50) {
            var user = UserBean()
            user.age = 18
            user.name = "测试" + i
            list.add(i, user)
        }
        return list
    }


    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(setDrawerGravity())) {
            drawer_layout.closeDrawer(setDrawerGravity())
        } else {
            super.onBackPressed()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.drawer, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        when (item.itemId) {
            R.id.action_settings -> return true
            else -> return super.onOptionsItemSelected(item)
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {
            R.id.nav_camera -> {
                // Handle the camera action
            }
            R.id.nav_gallery -> {

            }
            R.id.nav_slideshow -> {

            }
            R.id.nav_manage -> {

            }
            R.id.nav_share -> {

            }
            R.id.nav_send -> {

            }
        }

        drawer_layout.closeDrawer(setDrawerGravity())
        return true
    }


    //创建一个 伴生对象
    companion object {
        fun startDrawarLayoutActivity(context: Context) {

            val mIntent = Intent()
            mIntent.setClass(context, DrawerActivity::class.java)
            context.startActivity(mIntent)
        }
    }
}
